package com.cgw.game.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.Calendar;

/**
 * @Description TODO
 * @Author YF
 * @Date 2020/9/4
 * @Version 1.0
 */
public class SleepSurfaceView extends View {
    private Context context;
    /**
     * 进度画笔
     * */
    private Paint mPaint;

    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;
    /**
     * 圆环半径
     * */
    private float radius;
    private long[] time = new long[]{

    };
    /**
     * 弧线路径
     * */
    private Path path;
    public SleepSurfaceView(Context context) {
        this(context,null);
    }

    public SleepSurfaceView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SleepSurfaceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context= context;
        initView();
    }

    private void initView() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureSize(widthMeasureSpec);
        height = measureSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
    }
    /**
     * 初始化path路径
     * */
    private void initPath() {
        path = new Path();
        RectF oval = new RectF(-radius,-radius,radius,radius);
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < time.length; i++) {
            calendar.setTimeInMillis(time[i]);
            int start = calendar.get(Calendar.HOUR_OF_DAY)*60+calendar.get(Calendar.MINUTE);
            float startAngle = (start/1440f)*360f;
            calendar.setTimeInMillis(time[i+1]);
            int end = calendar.get(Calendar.HOUR_OF_DAY)*60+calendar.get(Calendar.MINUTE);
            if(start>end){
                end = end+(1440-start);
            }else {
                end = end -start;
            }
            float sweepAngle = (end/1440f)*360f;
            path.addArc(oval,startAngle,sweepAngle);
            i++;
        }
    }

    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = DensityUtil.dip2px(context,300);
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(width/2,height/2);
        drawScaleText(canvas);
        drawBottomCircular(canvas);
        drawProgress(canvas);
    }
    /**
    * 绘制四周时间
    */
    private void drawScaleText(Canvas canvas) {

    }
    /**
    * 绘制底部圆形
    */
    private void drawBottomCircular(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(40f);
        mPaint.setColor(Color.parseColor("#dcdedd"));
        canvas.drawCircle(0,0,width/2-20,mPaint);
    }

    public void setTime(long[] time) {
        this.time = time;
        invalidate();
    }

    /**
     * 根据进度画
     * */
    private void drawProgress(Canvas canvas) {
        canvas.save();
        canvas.rotate(-90);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(35f);
        mPaint.setColor(Color.parseColor("#5f88f0"));
        radius = width/2-20;
        initPath();
        PathMeasure measure = new PathMeasure();
        measure.setPath(path, false);
        Path dst = new Path();
        measure.getSegment(0, measure.getLength(), dst, true);
        canvas.drawPath(dst,mPaint);
        Log.e("size",measure.getLength()+"");
        measure.nextContour();
        measure.getSegment(0, measure.getLength(), dst, true);
//        canvas.drawArc(-radius,-radius,radius,radius,0,30,false,mPaint);
        canvas.drawPath(dst,mPaint);
        Log.e("size",measure.getLength()+"");
        canvas.restore();
        canvas.save();

    }
}
